/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gla.centralhotel.dao;

import gla.centralhotel.dao.interfaces.IRoomBookingDAO;
import gla.centralhotel.exception.CollectionRetrieverException;
import gla.centralhotel.model.Agency;
import gla.centralhotel.model.Customer;
import gla.centralhotel.model.Hotel;
import gla.centralhotel.model.RoomBooking;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Rivo
 */
@Stateless
public class RoomBookingDAO implements IRoomBookingDAO {
    @PersistenceContext
    private EntityManager em;

    public RoomBooking create(RoomBooking roomBooking) {
        em.persist(roomBooking);
        return roomBooking;
    }

    public RoomBooking edit(RoomBooking roomBooking) {
        em.merge(roomBooking);
        return roomBooking;
    }

    public void remove(RoomBooking roomBooking) {
        em.remove(em.merge(roomBooking));
    }

    public RoomBooking find(Object id) {
        return em.find(gla.centralhotel.model.RoomBooking.class, id);
    }

    public List<RoomBooking> findAll() {
        return em.createQuery("select object(o) from RoomBooking as o").getResultList();
    }
    
    /**
     * Recupere une arrayList de reservation pour une agance
     * @param agency
     * @return
     * @throws gla.centralhotel.exception.CollectionRetrieverException
     */
    public ArrayList<RoomBooking> getAllRoomBookingByAgency(Agency agency) throws CollectionRetrieverException{
        String query = "select rmBk from RoomBooking as rmBk where rmBk.refAgency = :refAgency";
        Query q = em.createQuery(query);
        q.setParameter("refAgency", agency);
        
        try{
            return new ArrayList(q.getResultList());
        }catch(IllegalStateException ex){
            ex.printStackTrace();
            throw new CollectionRetrieverException("Erreur de recuperation des reservations par agence");
        }
    }
    
    /**
     * Recupere une arrayList de reservation pour un customer
     * @param customer
     * @return
     * @throws gla.centralhotel.exception.CollectionRetrieverException
     */
    public ArrayList<RoomBooking> getAllRoomBookingByCustomer(Customer customer)throws CollectionRetrieverException{
        String query = "select rb from RoomBooking as rb where rb.refCustomer = :refCustomer";
        Query q = em.createQuery(query);
        q.setParameter("refCustomer", customer);

        try {
            return new ArrayList(q.getResultList());
        } catch (IllegalStateException ex) {
            ex.printStackTrace();
            throw new CollectionRetrieverException("Erreur de récupération des " +
                    "réservations en fonction du customer");
        }
    }

    public ArrayList<RoomBooking> getAllRoomBookingByHotel(Hotel hotel) throws CollectionRetrieverException{
        String query = "select rmBk from RoomBooking as rmBk where rmBk.refRoom.refHotel = :refHotel";
        Query q = em.createQuery(query);
        q.setParameter("refHotel", hotel);
        
        try{
            return new ArrayList(q.getResultList());
        }catch(IllegalStateException ex){
            ex.printStackTrace();
            throw new CollectionRetrieverException("Erreur de recuperation des reservations par hotel");
        }
    }
    
}
